<template>
  <div class="payment-result-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-section">
      <div class="loading-spinner"></div>
      <p>正在处理支付结果...</p>
    </div>

    <!-- 支付成功 -->
    <div v-else class="result-section success">
      <div class="result-icon">
        <i class="success-icon">✓</i>
      </div>
      <h2 class="result-title">支付成功！</h2>
      <p class="result-message">恭喜您，订单支付已完成</p>
      
      <!-- 订单详情表单 -->
      <div class="order-detail-form" v-if="orderDetail">
        <h3 class="form-title">订单详情</h3>
        
        <div class="detail-section">
          <h4>基本信息</h4>
          <div class="info-grid">
            <div class="info-item">
              <span class="label">订单号：</span>
              <span class="value">{{ orderDetail.orderId }}</span>
            </div>
            <div class="info-item">
              <span class="label">客户姓名：</span>
              <span class="value">{{ orderDetail.customerName }}</span>
            </div>
            <div class="info-item">
              <span class="label">订单总额：</span>
              <span class="value amount">¥{{ orderDetail.totalPrice }}</span>
            </div>
            <div class="info-item">
              <span class="label">创建时间：</span>
              <span class="value">{{ formatTime(orderDetail.createTime) }}</span>
            </div>
            <div class="info-item" v-if="orderDetail.payTime">
              <span class="label">支付时间：</span>
              <span class="value">{{ formatTime(orderDetail.payTime) }}</span>
            </div>
          </div>
        </div>

        <!-- 商品信息 -->
        <div class="detail-section" v-if="orderDetail.orderCustomerBySellers && orderDetail.orderCustomerBySellers.length > 0">
          <h4>商品信息</h4>
          <div v-for="seller in orderDetail.orderCustomerBySellers" :key="seller.sellerId" class="seller-section">
            <div class="seller-info">
              <span class="seller-name">商家：{{ seller.sellerName }}</span>
            </div>
            <div v-for="item in seller.orderItemVOList" :key="item.orderItemId" class="product-item">
              <div class="product-info">
                <img v-if="item.productImage" :src="item.productImage" :alt="item.productName" class="product-image">
                <div class="product-details">
                  <div class="product-name">{{ item.productName }}</div>
                  <div class="product-sku" v-if="item.skuName">规格：{{ item.skuName }}</div>
                  <div class="product-price">单价：¥{{ item.price }}</div>
                  <div class="product-quantity">数量：{{ item.quantity }}</div>
                  <div class="product-total">小计：¥{{ (item.price * item.quantity).toFixed(2) }}</div>
                </div>
              </div>
              <div class="delivery-info" v-if="item.address">
                <span class="label">收货地址：</span>
                <span class="value">{{ item.address }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="action-buttons">
        <button class="btn secondary" @click="goToOrderList">我的订单</button>
        <button class="btn secondary" @click="goToHome">继续购物</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 状态管理
const loading = ref(true)
const paymentStatus = ref('success') // 直接设置为成功
const orderDetail = ref(null)
const error = ref('')

// 从URL参数获取订单ID
const orderId = ref(route.query.orderId || route.query.out_trade_no || '未知订单')

// 页面加载时初始化
onMounted(async () => {
  if (orderId.value && orderId.value !== '未知订单') {
    await fetchOrderDetail()
  } else {
    loading.value = false
    error.value = '未找到订单信息'
  }
})

// 获取订单详情
const fetchOrderDetail = async () => {
  try {
    loading.value = true
    error.value = ''
    
    const token = localStorage.getItem('userToken')
    const headers = {
      'Content-Type': 'application/json'
    }
    
    if (token) {
      headers['Authorization'] = `Bearer ${token}`
    }
    
    const response = await fetch(`/api/orders/queryOrderMessage?orderId=${orderId.value}`, {
      method: 'GET',
      headers
    })
    
    if (response.ok) {
      const data = await response.json()
      orderDetail.value = data
    } else {
      error.value = '获取订单详情失败'
      console.error('获取订单详情失败:', response.status)
    }
  } catch (err) {
    error.value = '网络错误，请稍后重试'
    console.error('获取订单详情错误:', err)
  } finally {
    loading.value = false
  }
}

// 格式化时间
const formatTime = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 页面跳转方法
const goToOrderList = () => {
  router.push('/customer-orders')
}

const goToHome = () => {
  router.push('/')
}


</script>

<style scoped>
.payment-result-container {
  min-height: 100vh;
  overflow-y: auto;
  max-height: 100vh;
  padding: 40px 20px 60px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  width: 100%;
  box-sizing: border-box;
}

.loading-section {
  text-align: center;
  background: white;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  width: 85%;
  max-width: 600px;
  margin: 0 auto;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.result-section {
  background: white;
  padding: 60px 80px;
  border-radius: 20px;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.08);
  text-align: center;
  width: 85%;
  max-width: 1200px;
  margin: 0 auto 40px auto;
  position: relative;
  overflow: visible;
}

.result-icon {
  margin-bottom: 20px;
}

.success-icon {
  display: inline-block;
  width: 80px;
  height: 80px;
  background: #4CAF50;
  color: white;
  border-radius: 50%;
  line-height: 80px;
  font-size: 40px;
  font-weight: bold;
}

.result-title {
  color: #333;
  margin-bottom: 10px;
  font-size: 28px;
  font-weight: 600;
}

.result-message {
  color: #666;
  margin-bottom: 30px;
  font-size: 16px;
}

.order-detail-form {
  background: #f8f9fa;
  padding: 25px;
  border-radius: 10px;
  margin-bottom: 30px;
  text-align: left;
  margin-top: 40px;
  width: 100%;
}

.form-title {
  color: #333;
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: 600;
  text-align: center;
  border-bottom: 2px solid #667eea;
  padding-bottom: 10px;
}

.detail-section {
  margin-bottom: 25px;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.detail-section h4 {
  color: #333;
  margin-bottom: 15px;
  font-size: 16px;
  font-weight: 600;
  border-left: 4px solid #667eea;
  padding-left: 10px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
  margin-bottom: 30px;
  padding: 0 10px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
}

.info-item:last-child {
  border-bottom: none;
}

.label {
  font-weight: 600;
  color: #333;
  min-width: 80px;
}

.value {
  color: #666;
  text-align: right;
  flex: 1;
}

.amount {
  color: #e74c3c;
  font-weight: 600;
  font-size: 18px;
}

.seller-section {
  margin-bottom: 20px;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  overflow: hidden;
}

.seller-info {
  background: #f8f9fa;
  padding: 12px 15px;
  border-bottom: 1px solid #e9ecef;
}

.seller-name {
  font-weight: 600;
  color: #333;
}

.product-item {
  padding: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.product-item:last-child {
  border-bottom: none;
}

.product-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 0 10px;
}

.product-info {
  display: flex;
  gap: 15px;
  margin-bottom: 10px;
  padding: 20px 25px;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
  border-radius: 12px;
  border: 1px solid #e9ecef;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.product-info:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.product-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.product-details {
  flex: 1;
}

.product-name {
  font-weight: 600;
  color: #333;
  margin-bottom: 5px;
  font-size: 16px;
}

.product-sku,
.product-price,
.product-quantity,
.product-total {
  color: #666;
  margin-bottom: 3px;
  font-size: 14px;
}

.product-total {
  color: #e74c3c;
  font-weight: 600;
}

.delivery-info {
  background: #f8f9fa;
  padding: 10px;
  border-radius: 5px;
  font-size: 14px;
}

.action-buttons {
  display: flex;
  gap: 15px;
  justify-content: center;
  flex-wrap: wrap;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  display: inline-block;
}

.btn.primary {
  background: #667eea;
  color: white;
}

.btn.primary:hover {
  background: #5a6fd8;
  transform: translateY(-2px);
}

.btn.secondary {
  background: #f8f9fa;
  color: #333;
  border: 2px solid #ddd;
}

.btn.secondary:hover {
  background: #e9ecef;
  border-color: #ccc;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .payment-result-container {
    padding: 20px 10px 40px 10px;
  }
  
  .result-section {
    padding: 30px 20px;
    width: 95%;
  }
  
  .result-title {
    font-size: 24px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
    gap: 20px;
    padding: 0;
  }
  
  .product-info {
    flex-direction: column;
    padding: 15px;
  }
  
  .product-list {
    padding: 0;
  }
  
  .product-image {
    width: 100%;
    height: 200px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .btn {
    width: 100%;
  }
}
</style>